package com.googlecode.flair
{
	import com.googlecode.flair.components.IFlairComponent;
	import com.googlecode.flair.mvc.IView;
	import com.googlecode.flair.mvc.TitleWindowController;
	
	import flash.display.DisplayObject;
	
	import mx.core.UIComponent;
	import mx.managers.PopUpManager;
	
	[Bindable]
	public class DesignAdvisor extends TitleWindowController
	{
		
		public static var instance : DesignAdvisor = null;
		
		public var advisedComponent : UIComponent = null;
		private var designAdvisorView : DesignAdvisorView = null;
		
		public var initialized : Boolean = false;
		
		public var rootComponent : IFlairComponent = null;
		
		public function DesignAdvisor(designAdvisorView : DesignAdvisorView = null)
		{
			instance = this;
			this.designAdvisorView = designAdvisorView;
		}
		
		public final override function initialize(view:IView):void
		{
			this.designAdvisorView = view as DesignAdvisorView;
			initialized = true;
		}
		
		
		public final function showFor(parent : DisplayObject) : void {
			PopUpManager.removePopUp(designAdvisorView);
			PopUpManager.addPopUp(designAdvisorView, parent, false);
		} 
		
		public final override function showCloseButton():Boolean 
		{
			return false;
		}
		
		
		public final function showSelectedComponent(uiComponent : UIComponent) : Number 
		{
			return 2;
		}
		
		public final function removeSelected() : void {
			IFlairComponent(advisedComponent).removeFromParent();
			IFlairComponent(advisedComponent).parentComponent.select();
			if (designAdvisorView.liveDesignerOutline) { // was initialized
				designAdvisorView.liveDesignerOutline.invalidateList();
			}
		}
		
		public final function addComponent(component : Class) : void 
		{
			var uiComponent : UIComponent = new component() as UIComponent;
			if (advisedComponent is IFlairComponent && uiComponent is IFlairComponent) {
				IFlairComponent(advisedComponent).add(IFlairComponent(uiComponent));
			}
			if (designAdvisorView.liveDesignerOutline) { // was initialized
				designAdvisorView.liveDesignerOutline.invalidateList();
			}
		}

	}
	
}